package tool;

import org.junit.Test;

import java.io.*;

/**
 * @ClassName IsrAndOswDemo
 * @Description TODO
 * @Author LYJ
 * @Date 2018/12/23 22:03
 * @Verison 1.0
 **/
public class IsrAndOswDemo {

    @Test
    public void test01() throws IOException {
        FileInputStream inputStream = new FileInputStream("D:\\Hello.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
        FileOutputStream outputStream = new FileOutputStream("D:\\Hello8.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"utf-8");
        int read;
       /* while((read = inputStreamReader.read())!=-1){
            System.out.print((char)read);
        }*/
        char[] chars = new char[8*1024];
        while ((read=inputStreamReader.read(chars,0,chars.length))!=-1){
           String str = new String(chars,0,chars.length);
            System.out.println(str);
            outputStreamWriter.write(chars,0,read);
            outputStreamWriter.flush();
        }
        inputStreamReader.close();
        outputStreamWriter.close();
    }

    @Test
    public void test02() throws IOException{
        FileReader fileReader = new FileReader("D:\\Hello.txt");
        FileWriter fileWriter = new FileWriter("D:\\Hello11.txt");
        char[] chars = new char[1024];
        int read;
        while ((read = fileReader.read(chars,0,chars.length))!=-1) {
           fileWriter.write(chars,0,read);
           fileWriter.flush();
        }
        fileReader.close();
        fileWriter.close();
    }

    @Test
    public void test03() throws IOException{
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("D:\\Hello.txt")
                )
        );

        BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(
                        new FileOutputStream("D:\\Hello12.txt")
                )
        );

        char[] chars = new char[2*1024];
        int read;
        while((read = bufferedReader.read(chars,0,chars.length))!=-1){
            bufferedWriter.write(chars,0,read);
        }
        bufferedReader.close();
        bufferedWriter.close();

    }

    @Test
    public void test04() throws IOException{
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("D:\\Hello.txt")
                )
        );

        BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(
                        new FileOutputStream("D:\\Hello12.txt")
                )
        );

        String str;
        while ((str = bufferedReader.readLine())!=null){
            bufferedWriter.write(str);
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();

    }


    @Test
    public void test05() throws IOException{
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("D:\\Hello.txt")
                )
        );

        PrintWriter printWriter = new PrintWriter("D:\\Hello15.txt");

        String str;
        while ((str = bufferedReader.readLine())!=null){
          printWriter.println(str);
        }
        bufferedReader.close();
        printWriter.close();

    }



    @Test
    public void test06() throws Exception{
        ObjectOutputStream outputStream = new ObjectOutputStream(
                new FileOutputStream("D:\\student.dat")
        );
        Student student = new Student("小明",20,"男");
        outputStream.writeObject(student);
        outputStream.flush();
        outputStream.close();


        ObjectInputStream inputStream = new ObjectInputStream(
                new FileInputStream("D:\\student.dat")
        );

        Student stu = (Student) inputStream.readObject();
        System.out.println(stu.toString());
        inputStream.close();

    }




}
